<?php
require_once('config.inc.php');
require_once('DatabaseManager.php');

$db = new DatabaseManager;
$db -> createDB();

PRINT "<html>";
PRINT "<head><title>Benutzer-Daten eingeben</title></head>";
PRINT "<body>";

$dbname = DB_NAME;

if(!@$title = $_POST["title"])
		$title = '';
if(!@$first_name = $_POST["first_name"])
		$first_name = '';
if(!@$last_name = $_POST["last_name"])
		$last_name = '';
if(!@$postcode = $_POST["postcode"])
		$postcode = '';
if(!@$city = $_POST["city"])
		$city = '';
if(!@$country = $_POST["country"])
		$country = '';
if(!@$address = $_POST["address"])
		$address = '';
if(!@$phone = $_POST["phone"])
		$phone = '';
if(!@$position = $_POST["position"])
		$position = '';
if(!@$employer = $_POST["employer"])
		$employer = '';
if(!@$mail = $_POST["mail"])
		$mail = '';
if(!@$homepage = $_POST["homepage"])
		$homepage = '';

/* make connection to database */
MYSQL_CONNECT(DB_SERVER,DB_USER_NAME,DB_USER_PASSWORD) OR DIE("Datenbankverbindung konnte nicht hergestellt werden!");
@mysql_select_db("$dbname") or die("Datenbank nicht gefunden!"); 

PRINT "Folgende Daten wurden eingegeben:<br>";
PRINT "<ul>";
PRINT "<li>Titel: <em>$title</em>";
PRINT "<li>Vorname: <em>$first_name</em>";
PRINT "<li>Nachname: <em>$last_name</em>";
PRINT "<li>Adresse: <em>$address</em>";
PRINT "<li>PLZ: <em>$postcode</em>";
PRINT "<li>Ort: <em>$city</em>";
PRINT "<li>Land: <em>$country</em>";
PRINT "<li>Telefon: <em>$phone</em>";
PRINT "<li>E-Mail: <em>$mail</em>";
PRINT "<li>Homepage: <em>$homepage</em>";
PRINT "<li>Arbeitgeber: <em>$employer</em>";
PRINT "<li>Position: <em>$position</em>";
PRINT "</ul>";
PRINT "<hr>";

class InsertUserData
{
	function insertData($first_name, $last_name, $address, $postcode, $city, $country, $phone, $employer, $mail, $title, $homepage, $position)
	{
		/* MySQL table created to store the data */
		$tablename = "userdata";

		$query1 = "SELECT * FROM $tablename WHERE firstname = '$first_name' AND lastname = '$last_name'";
		$result1 = MYSQL_QUERY($query1);
	    $num_rows =  mysql_num_rows($result1);
		
		if(!$num_rows)
		{
			/* Insert information into table */
			$query2 = "INSERT INTO $tablename VALUES('NULL', '$first_name', '$last_name', '$address', $postcode, '$city', '$country', '$phone', '$employer', '$mail', '$title', '$homepage', '$position')";
			$result2 = MYSQL_QUERY($query2);
			
			PRINT "Antwort des Datenbank-Servers:<br>"; 
			if($result2)
			{
			  $affected_rows =  mysql_affected_rows();
			  $this->createUserDirectories(mysql_insert_id());
			  PRINT "<strong>Query OK. Betroffene Zeilen: $affected_rows</strong>";
			}
			else
			{
			  PRINT "<strong>Query FAILED. Sie haben möglicherweise nicht die benötigten Rechte!</strong>";
			}
		}
		else
		{
			PRINT "Benutzer ist bereits in der Datenbank!";
		}
	}
	
	function createUserDirectories($user_id)
	{
	  // FIXME: User Categories?
	  if (!file_exists(dirname(__FILE__)."/../users/"))
	    mkdir (dirname(__FILE__)."/../users/");
	            
	  mkdir(dirname(__FILE__)."/../users/".$user_id);
	  mkdir(dirname(__FILE__)."/../users/".$user_id."/home");
	  mkdir(dirname(__FILE__)."/../users/".$user_id."/templates");
	  mkdir(dirname(__FILE__)."/../users/".$user_id."/templates/1");
	  mkdir(dirname(__FILE__)."/../users/".$user_id."/templates/2");
	  mkdir(dirname(__FILE__)."/../users/".$user_id."/templates/3");
	}	
}

$userdata = new InsertUserData();
$userdata->insertData($first_name, $last_name, $address, $postcode, $city, $country, $phone, $employer, $mail, $title, $homepage, $position);

PRINT "<br>";
PRINT "<a href=\"index.php\">OK</a>";
PRINT "</body>";
PRINT "</html>";

/* Close the database connection */
MYSQL_CLOSE();
?>